package com.ruoyi.qxk.rabmq.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @className: yuYue
 * @author: xikun
 * @date: 2025/5/20 14:39
 * @Version: 1.0
 * @description:
 */
@Configuration
public class yuYue {
    //序列化器
    @Bean("yuYueMessageConverter")  // 添加唯一名称
    public MessageConverter yuYueMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }
    //声明正常交换机
    @Bean
    public Exchange  yuYueExchange1() {
        return ExchangeBuilder.topicExchange("yuYue_exchange1").durable(true).build();
    }
    //声明正常队列
    @Bean
    public Queue yuYueQueue1() {
        return QueueBuilder.durable("yuYue_queue1")
                .deadLetterExchange("yuYue_exchange2")//正常队列绑定死信交换机
                .deadLetterRoutingKey("yuYue.#")//正常队列绑定死信路由键
                .ttl(60*1000)  //设置消息的过期时间
                .build();
    }
    //绑定正常交换机和正常队列
    @Bean
    public Binding yuYueBinding1() {
        return BindingBuilder.bind(yuYueQueue1()).to(yuYueExchange1()).with("yuYue.#").noargs();
    }
    //声明死信交换机
    @Bean
    public Exchange yuYueExchange2() {
        return ExchangeBuilder.topicExchange("yuYue_exchange2").durable(true).build();
    }
    //声明死信队列
    @Bean
    public Queue yuYueQueue2() {
        return QueueBuilder.durable("yuYue_queue2").build();
    }
    //绑定死信交换机和死信队列
    @Bean
    public Binding yuYueBinding2() {
        return BindingBuilder.bind(yuYueQueue2()).to(yuYueExchange2()).with("yuYue.#").noargs();
    }
}
